package com.zhou.common.mybatis.template.shiro.zhou.aop;

/**
 * 在切面中做响应结果的封装
 */
/*

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.iwhalecloud.mgtcenter.common.errorcode.IErrorCode;
import com.iwhalecloud.mgtcenter.common.exception.BaseException;
import com.iwhalecloud.mgtcenter.entity.base.AjaxResult;

*/
/**
 * Created with IDEA ajaxResult封装aop
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/3/15
 * @time:17:41
 *//*

@Configuration
@Aspect
@Order(3)
public class AjaxResultPackageAop {

    private static final Logger logger = LoggerFactory.getLogger(AjaxResultPackageAop.class);
    */
/**
     * 使用缓存的实例避免重复创建对象
     *//*

    private static final AjaxResult OPERATION_FAIL = AjaxResult.fail(1000, "操作失败", null, null);

    */
/**
     * 切点为所有controller包下方法
     *//*

    @Pointcut("execution(public * com.iwhalecloud.mgtcenter.controller..*(..))")
    public void controllerPoint() {

    }

    */
/**
     * 将return参数修改成AjaxResult格式 修改返回值必须使用环绕增强
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     *//*

    @Around("controllerPoint()")
    public AjaxResult doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("AjaxResultPackageAop before joinPoint.proceed");
        // 写了环绕增加后 不执行proceed该切点的其他方法将不能够执行
        Object object;
        // 捕获所有执行中发生的异常并打印封装
        try {
            object = joinPoint.proceed();
        } catch (BaseException e) {
            IErrorCode errorCode = e.getErrorCode();
            return AjaxResult.fail(errorCode.errCode(), errorCode.errName(), errorCode.errName(), null);
        } catch (RuntimeException e) {
            return AjaxResult.fail(200, e.getMessage(), e.getMessage(), null);
        }
        catch (Exception e) {
            logger.error("exception in {}", joinPoint.getSignature(), e);
            return AjaxResult.fail(200, "操作失败", "unkonwn exception", null);
        }
        logger.info("AjaxResultPackageAop after joinPoint.proceed");
        // 如果已经是 AjaxResult 这种形式,不做处理直接返回
        if (object instanceof AjaxResult) {
            return (AjaxResult)object;
        }
        // 默认参数为 int 类型时 是操作数据库返回的修改行数,因此用该值判断是否操作成功
        // 规范不允许存在单一int值（除数据库操作的变更行数外）返回的情况
        if (object instanceof Integer) {
            int result = (int)object;
            if (result < 1) {
                // 操作失败
                return OPERATION_FAIL;
            } else {
                return AjaxResult.success();
            }
        }
        // 其他未知情况暂时先直接按success封装
        return AjaxResult.success(object);
    }

}
*/
